<?php defined('INDEX_FILE') or die('forbidden');


/**
* MLT Web Engine
* http://www.molotsoft.ru
*
* @package MLT_WE
* @subpackage Debug_Log
* @author Ivan Chistyakov <ivan@molotsoft.ru>
* @version $Id: debug_log.inc.php4 61 2009-02-20 10:45:29Z molotsoft@gmail.com $
* @link $HeadURL: http://debug-log.googlecode.com/svn/trunk/debug_log.inc.php4 $
*/


define('DBGL_PACKAGE', 'Debug_Log');
define('DBGL_VERSION', '1.0.2');

define('DBGL_PHP_VERSION', '4.0.7');
version_compare(PHP_VERSION, DBGL_PHP_VERSION, '>=') or trigger_error('required PHP_VERSION >= ' . DBGL_PHP_VERSION, E_USER_NOTICE);

define('DBGL_LFNC_VERSION', '1.0.0');
defined('LFNC_VERSION') and version_compare(LFNC_VERSION, DBGL_LFNC_VERSION, '>=') or trigger_error('required LFNC_VERSION >= ' . DBGL_LFNC_VERSION, E_USER_NOTICE);


define('DBGL_NL', NL); // lucky const
define('DBGL_DATETIME_FORMAT', DATETIME_FORMAT); // lucky const

class debug_log {

	var $levels = array(E_ERROR => 'Error', E_WARNING => 'Warning', E_NOTICE => 'Notice', E_USER_ERROR => 'User Error', E_USER_WARNING => 'User Warning', E_USER_NOTICE => 'User Notice');

	var $enabled = FALSE;

	var $message_type;
	var $destination;
	var $extra_headers;

	function debug_log($message_type = 0, $destination = '', $extra_headers = '') {
		$this->message_type = $message_type;
		$this->destination = $destination;
		$this->extra_headers = $extra_headers;
	}

	function enabled($enabled = NULL) {
		if (!is_null($enabled)) $this->enabled = (bool)$enabled;
		return $this->enabled;
	}

	function write($message) {
		return $this->enabled ? error_log(strip_tags($message), $this->message_type, $this->destination, $this->extra_headers) : FALSE;
	}

	function writeln($message) {
		return $this->write($message . DBGL_NL);
	}

	function write_date($message) {
		return $this->write(date(DBGL_DATETIME_FORMAT) . ' ' . $message);
	}

	function writeln_date($message) {
		return $this->writeln(date(DBGL_DATETIME_FORMAT) . ' ' . $message);
	}

	function send($message) {
		switch ($this->message_type) {
			case 0: return $this->write($message); break;
			case 1: return $this->write_date($message); break;
			case 2: return $this->write_date($message); break;
			case 3: return $this->writeln_date($message); break;
			default: return FALSE; break;
		}
	}

	function display_errors($class, $code, $message, $file, $line, $context, $halt_on_error = FALSE) {
		$message = sprintf_hsc('<br /><b>%s [%d]:</b> %s <br />in <b>%s</b> on line <b>%d</b> (ip <b>%s</b>)<br />', $class, $code, $message, $file, $line, ip()); // lucky func
		$this->send($message);
		if ((bool)ini_get('display_errors') && ((error_reporting() & $code) === $code)) echo $message . DBGL_NL;
		if (($code === E_USER_ERROR) && $halt_on_error) exit(1); // halt on error
		return TRUE;
	}

	function error_handler($errno, $errstr, $errfile, $errline, $errcontext) {
		return $this->display_errors(isset_in_array($this->levels, $errno, 'Unknown'), $errno, $errstr, $errfile, $errline, $errcontext, TRUE); // lucky func
	}

	function set_handler() {
		return set_error_handler(array(&$this, 'error_handler'));
	}

	function restore_handler() {
		return restore_error_handler();
	}

}


?>